<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>wordpress后台控制面板的几种玩法</title>
<link rel="stylesheet" href="../css/reset.css" type="text/css" />
<link rel="stylesheet" href="../css/prettify.css" type="text/css" />
<script type="text/javascript" src="../js/prettify.js"></script>
</head>

<body onload="prettyPrint()">
<h1><a href="http://www.wheatv.com/870.html" rel="bookmark" title="wordpress后台控制面板的几种玩法">WordPress后台控制面板的几种玩法</a></h1>
<hr />
<div class="entry">
<p>平时我们玩惯了<a href="http://www.wheatv.com" target="_blank"><strong>wordpress主题</strong></a>，有人肯定和我一样，开始想对<strong>wordpress后台控制面板</strong>动动手脚了，仔细研究下还真让你大吃一惊，wordpress的扩展性真是从前台到后台啊，下面介绍几种wordpress控制面板非常规的玩法。</p>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">删除控制板上的某个菜单</h3>
<pre class="prettyprint linenums">function remove_menus () {
global $menu;
        $restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
        end ($menu);
        while (prev($menu)){
            $value = explode(' ',$menu[key($menu)][0]);
            if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
        }
}
add_action('admin_menu', 'remove_menus');</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">更换属于自己的登录logo</h3>
<pre class="prettyprint linenums">function my_custom_login_logo() {
    echo '&lt;style type="text/css"&gt;
        h1 a { background-image:url('.get_bloginfo('template_directory').'
/images/custom-login-logo.gif) !important; }
    &lt;/style&gt;';
}
 
add_action('login_head', 'my_custom_login_logo');</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">更换后台控制面板的logo</h3>
<pre class="prettyprint linenums">add_action('admin_head', 'my_custom_logo');
 
function my_custom_logo() {
   echo '&lt;style type="text/css"&gt;
         #header-logo { background-image: url('.get_bloginfo('template_directory').'
/images/custom-logo.gif) !important; }&lt;/style&gt;';
}</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">关闭"请升级"提醒</h3>
<pre class="prettyprint linenums">if ( !current_user_can( 'edit_users' ) ) {
  add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );
  add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );
}</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">删除控制面板widget</h3>
<pre class="prettyprint linenums">function example_remove_dashboard_widgets() {
    // Globalize the metaboxes array, this holds all the widgets for wp-admin
     global $wp_meta_boxes;
 
    // Remove the incomming links widget
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);    
 
    // Remove right now
    unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
 
// Hoook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'example_remove_dashboard_widgets' );</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">在控制面板里添加自定义widget</h3>
<pre class="prettyprint linenums">function example_dashboard_widget_function() {
    // Display whatever it is you want to show
    echo "Hello World, I'm a great Dashboard Widget";
}
 
// Create the function use in the action hook
function example_add_dashboard_widgets() {
    wp_add_dashboard_widget('example_dashboard_widget', 'Example Dashboard Widget', 'example_dashboard_widget_function');
}
// Hoook into the 'wp_dashboard_setup' action to register our other functions
add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">为WordPress控制面板换色</h3>
<p>在不修改WordPress核心文件的情况下，也可以改变WordPress控制面板的颜色甚至字体或者版式。</p>
<p>下面的代码实现了一次基本的样式更改（将原本的灰色header变成蓝色），只要你愿意，其它的更改同样不在话下（在<!-- 和 -->标签之间进行编辑）。</p>
<pre class="prettyprint linenums">function custom_colors() {
   echo '&lt;style type="text/css"&gt;#wphead{background:#069}&lt;/style&gt;';
}
add_action('admin_head', 'custom_colors');</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">提供帮助性信息</h3>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">如果你是在帮助其他人开发WordPress网站，在控制面板的某些部分加上"帮助"说明必然是相当贴心的举动。</p>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">在functions.php文件里加上下面的代码， 让你的博客后台出现一些帮助说明类的温馨提示。</p>
<pre class="prettyprint linenums">function my_admin_help($text, $screen) {
    // Check we're only on my Settings page
    if (strcmp($screen, MY_PAGEHOOK) == 0 ) {
 
        $text = 'Here is some very useful information to help you use this plugin...';
        return $text;
    }
    // Let the default WP Dashboard help stuff through on other Admin pages
    return $text;
}
 
add_action( 'contextual_help', 'my_admin_help' );</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">在控制面板中监视服务器状况</h3>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">WordPress的控制面板API大大加强了控制板widget的实用性。有这样一个强大的widget可以实现在WordPress控制面板中直接监视服务器运行状态的功能。</p>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">也只需要在functions.php文件里加入下面的代码：</p>
<pre class="prettyprint linenums">function slt_PHPErrorsWidget() {
    $logfile = '/home/path/logs/php-errors.log'; // Enter the server path to your logs file here
    $displayErrorsLimit = 100; // The maximum number of errors to display in the widget
    $errorLengthLimit = 300; // The maximum number of characters to display for each error
    $fileCleared = false;
    $userCanClearLog = current_user_can( 'manage_options' );
    // Clear file?
    if ( $userCanClearLog &amp;&amp; isset( $_GET["slt-php-errors"] ) &amp;&amp; $_GET["slt-php-errors"]=="clear" ) {
        $handle = fopen( $logfile, "w" );
        fclose( $handle );
        $fileCleared = true;
    }
    // Read file
    if ( file_exists( $logfile ) ) {
        $errors = file( $logfile );
        $errors = array_reverse( $errors );
        if ( $fileCleared ) echo '&lt;p&gt;&lt;em&gt;File cleared.&lt;/em&gt;&lt;/p&gt;';
        if ( $errors ) {
            echo '&lt;p&gt;'.count( $errors ).' error';
            if ( $errors != 1 ) echo 's';
            echo '.';
            if ( $userCanClearLog ) echo ' [ &lt;b&gt;&lt;a href="'.get_bloginfo("url").'/wp-admin/?slt-php-errors=clear"
onclick="return confirm(\'Are you sure?\');"&gt;CLEAR LOG FILE&lt;/a&gt;&lt;/b&gt; ]';
            echo '&lt;/p&gt;';
            echo '&lt;div id="slt-php-errors" style="height:250px;overflow:scroll;padding:2px;background-color:
#faf9f7;border:1px solid #ccc;"&gt;';
            echo '&lt;ol style="padding:0;margin:0;"&gt;';
            $i = 0;
            foreach ( $errors as $error ) {
                echo '&lt;li style="padding:2px 4px 6px;border-bottom:1px solid #ececec;"&gt;';
                $errorOutput = preg_replace( '/\[([^\]]+)\]/', '&lt;b&gt;[$1]&lt;/b&gt;', $error, 1 );
                if ( strlen( $errorOutput ) &gt; $errorLengthLimit ) {
                    echo substr( $errorOutput, 0, $errorLengthLimit ).' [...]';
                } else {
                    echo $errorOutput;
                }
                echo '&lt;/li&gt;';
                $i++;
                if ( $i &gt; $displayErrorsLimit ) {
                    echo '&lt;li style="padding:2px;border-bottom:2px solid #ccc;"&gt;&lt;em&gt;More than '.$displayErrorsLimit.'
errors in log...&lt;/em&gt;&lt;/li&gt;';
                    break;
                }
            }
            echo '&lt;/ol&gt;&lt;/div&gt;';
        } else {
            echo '&lt;p&gt;No errors currently logged.&lt;/p&gt;';
        }
    } else {
        echo '&lt;p&gt;&lt;em&gt;There was a problem reading the error log file.&lt;/em&gt;&lt;/p&gt;';
    }
}
 
// Add widgets
function slt_dashboardWidgets() {
    wp_add_dashboard_widget( 'slt-php-errors', 'PHP errors', 'slt_PHPErrorsWidget' );
}
add_action( 'wp_dashboard_setup', 'slt_dashboardWidgets' );</pre>
<h3 style="margin-bottom: 0.3em; font-size: 1.1em;">根据用户角色移除相应的widget</h3>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">针对多用户博客。</p>
<p style="margin-top: 0.4em; margin-bottom: 0.8em;">下面的代码会将meta框postcustom从"作者"角色的控制板上删除。 同样你只需要在functions.php文件里加上下面的代码：</p>
<pre class="prettyprint linenums">function customize_meta_boxes() {
     //retrieve current user info
     global $current_user;
     get_currentuserinfo();
 
     //if current user level is less than 3, remove the postcustom meta box
     if ($current_user-&gt;user_level &lt; 3)
          remove_meta_box('postcustom','post','normal');
}
 
add_action('admin_init','customize_meta_boxes');</pre>
<p>以上代码转自wordpress.la，使用方法：加入到主题的functions.php中。</p>
</body>
</html>
